const ASRClient = require('./client-backend/asrClient');
const fs = require('fs');
const path = require('path');

// 测试配置
const TEST_AUDIO_FILE = path.join(__dirname, 'test_audio.wav');
const SERVER_URL = 'ws://localhost:8888';

async function runTests() {
  console.log('=== 开始ASR客户端测试 ===');
  
  // 1. 测试连接
  console.log('\n[测试1] 测试服务器连接...');
  const client = new ASRClient(SERVER_URL);
  
  await new Promise(resolve => {
    client.ws.on('open', () => {
      console.log('✓ 连接成功');
      resolve();
    });
    
    client.ws.on('error', (err) => {
      console.error('✗ 连接失败:', err.message);
      process.exit(1);
    });
  });

  // 2. 测试模式选择
  console.log('\n[测试2] 测试模式选择...');
  client.selectMode('realtime');
  console.log('✓ 实时模式选择命令已发送');
  
  // 3. 测试音频发送 (如果有测试音频文件)
  if (fs.existsSync(TEST_AUDIO_FILE)) {
    console.log('\n[测试3] 测试音频文件发送...');
    const audioData = fs.readFileSync(TEST_AUDIO_FILE);
    client.uploadFile(audioData, 'test_audio.wav');
    console.log('✓ 测试音频文件已发送');
  } else {
    console.log('\n[跳过测试3] 未找到测试音频文件 (test_audio.wav)');
  }

  // 4. 测试断开连接
  console.log('\n[测试4] 测试断开连接...');
  client.ws.close();
  console.log('✓ 连接已关闭');

  console.log('\n=== 测试完成 ===');
}

runTests().catch(err => {
  console.error('测试失败:', err);
  process.exit(1);
});